package com.pine.app.module.security.oauth.provider.client;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @program: pine-security
 * @description:
 * @author: xyjxust
 * @create: 2019-09-05 22:07
 **/
@Data
public class BaseClient implements ClientDetails {


    @JsonProperty("client_id")
    private String clientId;

    @JsonProperty("client_secret")
    private String clientSecret;
    @JsonProperty("client_name")
    private String clientName;

    private String description;

    private Set<String> scope;

    @JsonProperty("resource_ids")
    private Set<String> resources;

    @JsonProperty("authorized_grant_types")
    private Set<String> authorizedGrantTypes;

    @JsonProperty("redirect_uri")
    private Set<String> registeredRedirectUris;

    @JsonProperty("autoapprove")
    private Set<String> autoApproveScopes;

    private Set<String> roles;

    @JsonProperty("access_token_validity")
    private Integer accessTokenValiditySeconds;

    @JsonProperty("refresh_token_validity")
    private Integer refreshTokenValiditySeconds;

    @JsonIgnore
    private Map<String, Object> additionalInformation;

    @JsonIgnore
    private Long expiredTime;


    @Override
    public String getClientId() {
        return this.clientId;
    }

    @Override
    public boolean isSecretRequired() {
        return this.clientSecret != null;
    }

    @Override
    public String getClientSecret() {
        return this.clientSecret;
    }

    @Override
    public boolean isScoped() {
        return this.scope != null && !this.scope.isEmpty();
    }

    @Override
    public Set<String> getScope() {
        return this.scope;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return this.authorizedGrantTypes;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return this.registeredRedirectUris;
    }


    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String var1) {
        if (this.autoApproveScopes == null) {
            return false;
        }
        Iterator var2 = this.autoApproveScopes.iterator();

        String auto;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            auto = (String) var2.next();
        } while (!auto.equals("true") && !scope.contains(auto));

        return true;

    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return this.additionalInformation;
    }

    @Override
    public boolean isExpiredTime() {
        if (expiredTime == null || expiredTime==0) {
            return false;
        }

        return expiredTime < System.currentTimeMillis();
    }
}
